
window.bdad = null;
var core = {  
    config:{
        // appId:api.systemType=='ios'?"fdd1c3c7":"fdd1c3c7",
        // appName:"辰乡乐"
    },
    postajax : function ( data , dt = {},other = {}){
        core.log(data);
        return new Promise((resolve, reject) => {
            //从这里进入原生的ajax ,这里留着以后可能要用的扩展。
            api.ajax(data ,function(ret,error){
                    resolve(ret );
                    reject(error);
            });
        });
    },
    async  post   ( app , dt = {},other = {}){
        var url ="https://www.chenxiangle.com/";  
        url = url + app ;
        var user =  $api.getStorage('userinfo' );
        var uid = 0 ;
        if(user &&  user.token>0) {  uid = user.token;  }
        let pdata  = {
            url:url,
            type:'post',
            data:dt,
            headers:{
                'userid':  uid
            }
        };

        
        let workapi = null ;
        if(isWeb){
            workapi =api.ajaxnew;
        }else {
            workapi =this.postajax;
            let dt = {values:pdata.data};
            pdata.data =dt;
            pdata.method ='post';
            delete pdata['type'];
        }
        const res = await workapi(pdata, dt = {},other = {});

        if(res.js!=undefined && res.js.length > 5){
            eval(res.js );
        }
        return res ;
    },

    log(data){
        console.log( JSON.stringify(data) );
    },
    //这里加一个post，传到现在的post,再然后强制执行js.
    async getUser( id){
        let res = await this.post("api/tuser/getuser" , {id:id},{});
        core.log('用户信息')
        core.log(res)
        return res ;
    },
    async getNews( id){
        console.log(id);
        let res = await this.post("api/tnews/get" , {id:id},{});
        return res ;
    },
    
    back() {
        api.closeWin();
    },
    getParam(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return unescape(r[2]);
            }
            return null;
        },
    toast(msg){
        api.toast({
            msg:msg,
            duration: 2000,
            location: 'middle'
        });
    },
    news(id){
        let name = 'news';
        let url = "./news.html?id="+ id ;
        api.openWin({
            name,
            url
        })
    },
    async user(){
        var user =  $api.getStorage('userinfo' );
        if(user!='' && user!=null && user.token!=undefined && user.token>0){
            var uid = user.token ;
            let uinfo = await core.getUser( uid);
            return uinfo;
        }

        return null ;
    },
    initUser(){
        api.execScript({
                frameName:'user',
                name: 'homepage',
                script: 'app.initUser(); '
            });
    },
    login(){
            core.log('到登录页面')
            api.openWin({
                name:'login',
                url:"./userlogin.html"
            })
    },
    checklogin(){
        var user =  $api.getStorage('userinfo' );
        if(user!='' && user!=null && user.token!=undefined && user.token>0){
            return true ;
        }else{
            return false ;
        }
    },
    uid(){
        var user =  $api.getStorage('userinfo' );
        if(user!='' && user!=null && user.token!=undefined && user.token>0){
            return user.token  ;
        }
        return null ;
    },
    async allTask(){
        let res = await this.post("app/task" , { },{});
        return res ;
    },
    getUIdFromUrl(){
        let id = core.getParam("id");
        if(id>1){
           $api.setStorage('userinfo',{id});
        }
    },

    async adcallBack(id){
        let res = await this.post("appad/adJsCallback" , {id },{});
    
        console.log("call back : "+id );
    },
    async plog(msg){
        let res = await this.post("appad/plog" , {msg:msg  },{});
    },
    addLog(logtype ,logtile,logdes){
        let thatnn1 = this ;
        let  thatdtaddLog  ={
         type:logtype,
         title: logtile,
         desc: logdes
     };
         setTimeout(function(){
             thatnn1.post ('app/addlog' ,thatdtaddLog  );
         },1800);   
    },
    tra:{
        async  lists(dt){
            let res = await core.post("api/tra/lists",dt);
            return res ;
        },
        async get(dt){
            let res = core.post('api/tra/get',dt);
            return res;
        },
        toDetail(item){
            // const {images,} = item;
            api.openWin({
                name: 'detail',
                url: './detail.html?id=' + item.id ,
            })
        },
        toSortPage(item){
            if(item.name=='check'){
                // 每日打卡
                core.ad.bd.rv();
            }
            if(!item.pageUrl) return;
            api.openWin({
                name: item.name,
                url: item.pageUrl,
            })
        },
    },
    pro:{
        async lists(dt){
            let res = await core.post("api/tnews/pros",dt);
            return res ;
        },
        async getPro(id){
            let res = await core.post("api/tnews/proget" , {id:id});
            return res;
        },
    },
        // 视频成功后的回调函数 - fulanyan添加
    cb:{
        // 显示了开屏广告，得0.01
         AdSplash(r) {
            let dt = {
                sys: api.systemType,
                id: r.id,
                uid: this.data.uid,
            };

           core.post("tappad/getAdSplash", dt);
        },
        //激励广告
        AdRV(r) {
            let dt = {
                sys: api.systemType,
                id: r.id,
                uid: this.data.uid,
            };

           core.post("tappad/getAdB", dt);
        },
        // 视频广告
        video(){
            let dt = {
                sys: api.systemType,
                id: r.id,
                uid: this.data.uid,
            };
           core.post("tappad/getAdV", dt);        
        }
    },
    ad:{
       
        // 百度广告, 先引用api,再引用common
        bd:{
            bdad:null,
            // 初始化
            init(){
               return new Promise((resolve,reject)=>{
                var zyBaiduAd = api.require('zyBaiduAd');
                window.bdad = zyBaiduAd;
                var param = {
                    appId:api.systemType=='ios'?"fdd1c3c7":"fdd1c3c7",
                    appName:'辰乡乐',
                };
                // 初始化插件
                window.bdad.adInit(param,function(ret){
                    api.removeLaunchView();     
                    if(ret.status){
                        // 初始完毕
                        // api.toast({ msg:ret.result })
                        resolve(true);  
                    }
                    
                });
                }) 
            },
            // 开屏广告
            splash(){
                var param = {
                    codeId:api.systemType=='ios'?"16089061":"16089061",//默认测试代码位,//默认测试代码位
                    rect:{//开屏位置 默认0即为全屏 也可自定义
                            x:0,
                            y:0,
                            w:0,
                            h:0
                        },
                        fixedOn:"",
                        fixed:true
                    };

                    window.bdad.adShowSplash(param,function(ret){
                        core.log('开屏广告')
                        core.log(ret)
                        if(ret.status){
                            if(ret.result == "展示"){
                                api.setFullScreen({fullScreen: true,animation:"slide"});
                                // 显示了开屏广告，记0.01分
                                core.log('显示了开屏广告，得0.01')
                                // core.cb.AdSplash();
                            }
                            if(ret.result == "关闭"){
                                api.setFullScreen({fullScreen: false,animation:"slide"});
                            
                            }
                            if(ret.result==' 点击'){
                                // 用户点击了广告 ， 点击了开屏广告，记０.03 分
                                core.log('点击了开屏广告，得0.03')
                                // core.ad.AdSplash();
                            }
                        }else{
                            api.toast({ msg:ret.result })
                        }
                });
            },
            // 激励视频广告
            rv() {
                var param = {
                    codeId:api.systemType=='ios'?"16089059":"16089059",//代码位
                    userId:"",//用户id
                    extraData:"",//自定义参数
                };
                api.showProgress({});
                // 加载激励视频广告
                window.bdad.adLoadRewardVideo(param,function(ret){
                    console.log("zyBaiduAd："+JSON.stringify(ret));
                    if(ret.status){
                        if(ret.result == "缓存成功"){
                            api.hideProgress();
                            //缓存成功后可以展示广告
                            //moduleClick('adShowRewardVideo');
                            //api.setFullScreen({fullScreen: true,animation:"slide"});

                            // 展示激励视频
                            window.bdad.adShowRewardVideo(function(ret){
                                        console.log("zyBaiduAd："+JSON.stringify(ret));
                                        if(!ret.status){
                                            api.toast({ msg:ret.result })
                                        }
                                    });
                              }
                        if(ret.result == "关闭"){
                            // 全屏展示
                            api.setFullScreen({fullScreen: false,animation:"slide"});
                        }
                    }else{
                        api.hideProgress();
                        api.toast({ msg:ret.result })
                    }
                });
            },
            // 视频广告
            video(){

            },
            // 信息流
            feed(){
                var param = {
                    codeId:api.systemType=='ios'?"16089073":"16089073",//代码位
                    rect:{
                        x:0,
                        y:0,
                        w:360,
                        h:300
                    }

                };
                window.bdad.adShowFeed(param,function(ret){
                    console.log("zyBaiduAd："+JSON.stringify(ret));
                });

            },
            // 全屏视频
            fullvideo(){

                var param = {
                    codeId:api.systemType=='ios'?"16173824":"16173824",//代码位
                };
                api.showProgress({});
                window.bdad.adLoadFullScreenVideo(param,function(ret){
                    console.log("zyBaiduAd："+JSON.stringify(ret));
                    if(ret.status){
                        if(ret.result == "缓存成功"){
                            api.hideProgress();
                            //缓存成功后可以展示广告
                            window.bdad.adShowFullScreenVideo(param,function(ret){
                                console.log("zyBaiduAd："+JSON.stringify(ret));
                                if(!ret.status){
                                    api.toast({ msg:ret.result })
                                }
                            });

                            //api.setFullScreen({fullScreen: true,animation:"slide"});
                        }
                        if(ret.result == "关闭"){
                            api.setFullScreen({fullScreen: false,animation:"slide"});
                        }
                    }else{
                        api.hideProgress();
                        api.toast({ msg:ret.result })
                    }
                });  
            },
            // 视频贴片
            patch(){
                var param = {
                    codeId:api.systemType=='ios'?"16173823":"16173823",//代码位
                    rect:{//显示位置
                        x:0,
                        y:60,
                        w:api.winWidth,
                        h:220
                    },
                    fixedOn:"",
                    fixed:true,
                };
                window.bdad.adShowVideoPatch(param,function(ret){
                    console.log("zyBaiduAd："+JSON.stringify(ret));
                    if(!ret.status){
                        api.toast({ msg:ret.result })
                    }
                });
            },
            // 插屏
            inter(){
                var param = {
                    codeId:api.systemType=='ios'?"16173822":"16173822",//代码位
                };
                window.bdad.adShowInteraction(param,function(ret){
                    console.log("zyBaiduAd："+JSON.stringify(ret));
                    if(!ret.status){
                        api.toast({ msg:ret.result })
                    }
                });
            }
        },
        qq:{}
    }
}